//
//  WKWbCrossDomainHandle.m
//  GSDache
//
//  Created by tanyin on 16/12/30.
//  Copyright © 2016年 tanyin. All rights reserved.
//

#import "WKWbCrossDomainHandle.h"

@implementation WKWbCrossDomainHandle

+ (WKNavigationActionPolicy)crossDomainHandleWebView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction useRequest:(void(^)(NSURLRequest *request))allowRequest{
    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
    NSURLRequest *request = navigationAction.request;
    NSURL *requestURL = request.URL;
    NSLog(@"to url:%@", request.URL.absoluteString);
    //需要对跨域做单独处理 默认http 和 https 不做跨域处理
    NSString *scheme = requestURL.scheme;
    if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"] && ![requestURL.absoluteString isEqualToString:@"about:blank"] && scheme) {
        //检测 妈蛋 要面对各种情况 phoneto://095196166
        BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:requestURL];
        if (canOpenURL) {
            [self openURL:requestURL];
        }else {
            //此处需要先处理 是不是自己服务器特定的跨域方式
            [self handleServerCrossDomain:requestURL];
            //[self showErrorAlertWithString:requestURL.absoluteString];
        }
        policy = WKNavigationActionPolicyCancel;
    }else {
        //如果跨域请求会crash
        if (![requestURL.absoluteString isEqualToString:@"about:blank"]) {
            if (allowRequest) {
                allowRequest(request);
            }
        }
    }
    return policy;
}

+ (void)showErrorAlertWithString:(NSString *)string {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请求出现问题" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];
    [alert show];
}
//
+ (void)openURL:(NSURL *)url {
    if (@available(iOS 10.0,*)) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
            
        }];
    }else {
        [[UIApplication sharedApplication] openURL:url];
    }
}
//处理自己服务器跨域方式
+ (void)handleServerCrossDomain:(NSURL *)url {
    NSString *scheme = url.scheme;
    NSLog(@"relativePath  %@ ",url.relativePath);
    NSLog(@"absoluteString  %@ ",url.absoluteString);
    NSLog(@"resourceSpecifier  %@ ",url.resourceSpecifier);
    NSLog(@"host  %@ ",url.host);
    NSLog(@"parameterString  %@ ",url.parameterString);
    NSLog(@"fragment  %@ ",url.fragment);
    NSLog(@"query  %@ ",url.query);
    NSLog(@"path  %@ ",url.path);
    //服务器特定打电话 scheme
    if ([scheme isEqualToString:@"phoneto"]) {
        NSString *host = url.host;
        NSString *tel = [NSString stringWithFormat:@"tel://%@",host];
        [self openURL:[NSURL URLWithString:tel]];
        return;
    }
    //解析可能是电话
//    NSArray *hostArr = [url.host componentsSeparatedByString:@"/"];
//    if (hostArr && hostArr.count==1) {
//        NSString *host = hostArr[0];
//        NSString *tel = [NSString stringWithFormat:@"tel://%@",host];
//        [self openURL:[NSURL URLWithString:tel]];
//        return;
//    }
    
    //忽略 about:blank
    if ([url.absoluteString isEqualToString:@"about:blank"]) {
        return;
    }
    
    /**
     是否要解析支付宝 微信的 跨域 待测试
     */
    [self showErrorAlertWithString:url.absoluteString];
}

@end
